package com.star.controller.business;

import com.star.dto.CategoryDTO;
import com.star.dto.CategoryPageQueryDTO;
import com.star.entity.Category;
import com.star.result.PageResult;
import com.star.result.Result;
import com.star.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理
 */
@RestController("businessCategoryController")
@RequestMapping("/business/category")
@Slf4j
// Swagger接口文档
@Api(tags = "商家端菜单分类接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param categoryDTO body参数
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result<String> addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类{}", categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO query参数
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分类分页查询{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询分类
     * @param id 路径参数
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询分类")
    public Result<Category> getById(@PathVariable Long id) {
        log.info("分类id{}", id);
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 更新分类
     * @param categoryDTO body参数
     * @return
     */
    @PutMapping
    @ApiOperation("更新分类")
    public Result<String> update(@RequestBody CategoryDTO categoryDTO) {
        log.info("更新分类{}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 更新分类状态
     * @param status 路径参数
     * @param id query参数
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("更新分类状态")
    public Result<String> updateStatus(@PathVariable Integer status, Long id) {
        log.info("更新分类状态{}{}", status, id);
        categoryService.updateStatus(status, id);
        return Result.success();
    }

    /**
     * 根据id删除分类
     * @param id query参数
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result<String> delete(Long id) {
        log.info("删除分类{}", id);
        categoryService.delete(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> getByType(Long businessId,Integer type) {
        log.info("根据类型查询分类{}{}", businessId,type);
        List<Category> categoryList = categoryService.getByType(businessId, type);
        return Result.success(categoryList);
    }
}
